﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace Model
{
    [Serializable()]
    public class ThuChiThuQuyReduceBO : Csla.BusinessBase<ThuChiThuQuyReduceBO>
    {
        #region Business Properties

        private int? _ThuChiThuQuyID = null;
        public int? ThuChiThuQuyID
        {
            get { return this._ThuChiThuQuyID; }
            set { this._ThuChiThuQuyID = value; }
        }

        private int? _LoaiPhieu = null;
        public int? LoaiPhieu
        {
            get { return this._LoaiPhieu; }
            set { this._LoaiPhieu = value; }
        }

        private string _BoPhan = null;
        public string BoPhan
        {
            get { return this._BoPhan; }
            set { this._BoPhan = value; }
        }

        private DateTime? _NgayLapPhieu = null;
        public DateTime? NgayLapPhieu
        {
            get { return this._NgayLapPhieu; }
            set { this._NgayLapPhieu = value; }
        }

        private DateTime? _NgayKyNhan = null;
        public DateTime? NgayKyNhan
        {
            get { return this._NgayKyNhan; }
            set { this._NgayKyNhan = value; }
        }

        private string _QuyenSo = null;
        public string QuyenSo
        {
            get { return this._QuyenSo; }
            set { this._QuyenSo = value; }
        }

        private int? _HoaDonBanID = null;
        public int? HoaDonBanID
        {
            get { return this._HoaDonBanID; }
            set { this._HoaDonBanID = value; }
        }

        private string _So = null;
        public string So
        {
            get { return this._So; }
            set { this._So = value; }
        }

        private string _No = null;
        public string No
        {
            get { return this._No; }
            set { this._No = value; }
        }

        private string _Co = null;
        public string Co
        {
            get { return this._Co; }
            set { this._Co = value; }
        }

        private int? _KhachHangID = null;
        public int? KhachHangID
        {
            get { return this._KhachHangID; }
            set { this._KhachHangID = value; }
        }

        private bool? _LaKhachHang = false;
        public bool? LaKhachHang
        {
            get { return this._LaKhachHang; }
            set { this._LaKhachHang = value; }
        }

        private string _HoTen = null;
        public string HoTen
        {
            get { return this._HoTen; }
            set { this._HoTen = value; }
        }

        private string _DiaChi = null;
        public string DiaChi
        {
            get { return this._DiaChi; }
            set { this._DiaChi = value; }
        }

        private string _LyDo = null;
        public string LyDo
        {
            get { return this._LyDo; }
            set { this._LyDo = value; }
        }

        private double? _SoTien = null;
        public double? SoTien
        {
            get { return this._SoTien; }
            set { this._SoTien = value; }
        }

        private string _ChungTuGocKemTheo = null;
        public string ChungTuGocKemTheo
        {
            get { return this._ChungTuGocKemTheo; }
            set { this._ChungTuGocKemTheo = value; }
        }

        private int? _GiamDocID = null;
        public int? GiamDocID
        {
            get { return this._GiamDocID; }
            set { this._GiamDocID = value; }
        }

        private int? _KeToanTruongID = null;
        public int? KeToanTruongID
        {
            get { return this._KeToanTruongID; }
            set { this._KeToanTruongID = value; }
        }

        private string _NguoiNopTien = null;
        public string NguoiNopTien
        {
            get { return this._NguoiNopTien; }
            set { this._NguoiNopTien = value; }
        }

        private int? _NguoiLapPhieuID = null;
        public int? NguoiLapPhieuID
        {
            get { return this._NguoiLapPhieuID; }
            set { this._NguoiLapPhieuID = value; }
        }

        private int? _ThuQuyID = null;
        public int? ThuQuyID
        {
            get { return this._ThuQuyID; }
            set { this._ThuQuyID = value; }
        }

        private string _DaNhanDuSoTien = null;
        public string DaNhanDuSoTien
        {
            get { return this._DaNhanDuSoTien; }
            set { this._DaNhanDuSoTien = value; }
        }

        private double? _TyGiaNgoaiTe = null;
        public double? TyGiaNgoaiTe
        {
            get { return this._TyGiaNgoaiTe; }
            set { this._TyGiaNgoaiTe = value; }
        }

        private double? _SoTienQuyDoi = null;
        public double? SoTienQuyDoi
        {
            get { return this._SoTienQuyDoi; }
            set { this._SoTienQuyDoi = value; }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        private int? _INgay = 0;
        public int? INgay
        {
            get { return _INgay; }
            set { _INgay = value; }
        }

        private string _StrLoaiPhieu = null;
        public string StrLoaiPhieu
        {
            get { return this._StrLoaiPhieu; }
            set { this._StrLoaiPhieu = value; }
        }

        protected override object GetIdValue()
        {
            return _ThuChiThuQuyID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newThuChiThuQuyID = -1;
        private ThuChiThuQuyReduceBO()
        {
            _ThuChiThuQuyID = newThuChiThuQuyID--;
        }

        public ThuChiThuQuyReduceBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public ThuChiThuQuyReduceBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static ThuChiThuQuyReduceBO NewRootThuChiThuQuyReduceBO()
        {
            return DataPortal.Create<ThuChiThuQuyReduceBO>(new RootCriteria());
        }

        internal static ThuChiThuQuyReduceBO NewChildThuChiThuQuyReduceBO()
        {
            return DataPortal.Create<ThuChiThuQuyReduceBO>(new ChildCriteria());
        }

        public static ThuChiThuQuyReduceBO GetRootThuChiThuQuyReduceBO(int _ThuChiThuQuyID)
        {
            return DataPortal.Fetch<ThuChiThuQuyReduceBO>(new RootCriteria(_ThuChiThuQuyID));
        }

        public static ThuChiThuQuyReduceBO GetRootThuChiThuQuyReduceBOByWhere(string where)
        {
            return DataPortal.Fetch<ThuChiThuQuyReduceBO>(where);
        }

        internal static ThuChiThuQuyReduceBO GetChildThuChiThuQuyReduceBO(SqlDataReader dataReader)
        {
            return new ThuChiThuQuyReduceBO(dataReader);
        }

        public static void DeleteThuChiThuQuyReduceBO(int _ThuChiThuQuyID)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiThuQuyID));
        }

        public static void DeleteThuChiThuQuyReduceBO(int _ThuChiThuQuyID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_ThuChiThuQuyID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int ThuChiThuQuyID;
            public bool directDelete = false;
            public RootCriteria(int _ThuChiThuQuyID)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = false;
            }

            public RootCriteria(int _ThuChiThuQuyID, bool directDelete)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int ThuChiThuQuyID;
            public bool directDelete = false;
            public ChildCriteria(int _ThuChiThuQuyID)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = false;
            }

            public ChildCriteria(int _ThuChiThuQuyID, bool directDelete)
            {
                this.ThuChiThuQuyID = _ThuChiThuQuyID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiThuQuyReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[ThuChiThuQuyID] = " + criteria.ThuChiThuQuyID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("ThuChiThuQuyReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            if (!(dataReader["ThuChiThuQuyID"] is DBNull))
                this._ThuChiThuQuyID = (int)dataReader["ThuChiThuQuyID"];
            if (!(dataReader["LoaiPhieu"] is DBNull))
                this._LoaiPhieu = (int)dataReader["LoaiPhieu"];
            if (!(dataReader["BoPhan"] is DBNull))
                this._BoPhan = (string)dataReader["BoPhan"];
            if (!(dataReader["NgayLapPhieu"] is DBNull))
                this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];
            if (!(dataReader["NgayKyNhan"] is DBNull))
                this._NgayKyNhan = (DateTime)dataReader["NgayKyNhan"];
            if (!(dataReader["QuyenSo"] is DBNull))
                this._QuyenSo = (string)dataReader["QuyenSo"];
            if (!(dataReader["HoaDonBanID"] is DBNull))
                this._HoaDonBanID = (int)dataReader["HoaDonBanID"];
            if (!(dataReader["So"] is DBNull))
                this._So = (string)dataReader["So"];
            if (!(dataReader["No"] is DBNull))
                this._No = (string)dataReader["No"];
            if (!(dataReader["Co"] is DBNull))
                this._Co = (string)dataReader["Co"];
            if (!(dataReader["KhachHangID"] is DBNull))
                this._KhachHangID = (int)dataReader["KhachHangID"];
            if (!(dataReader["LaKhachHang"] is DBNull))
                this._LaKhachHang = (bool)dataReader["LaKhachHang"];
            if (!(dataReader["HoTen"] is DBNull))
                this._HoTen = (string)dataReader["HoTen"];
            if (!(dataReader["DiaChi"] is DBNull))
                this._DiaChi = (string)dataReader["DiaChi"];
            if (!(dataReader["LyDo"] is DBNull))
                this._LyDo = (string)dataReader["LyDo"];
            if (!(dataReader["SoTien"] is DBNull))
                this._SoTien = (double)dataReader["SoTien"];
            if (!(dataReader["ChungTuGocKemTheo"] is DBNull))
                this._ChungTuGocKemTheo = (string)dataReader["ChungTuGocKemTheo"];
            if (!(dataReader["GiamDocID"] is DBNull))
                this._GiamDocID = (int)dataReader["GiamDocID"];
            if (!(dataReader["KeToanTruongID"] is DBNull))
                this._KeToanTruongID = (int)dataReader["KeToanTruongID"];
            if (!(dataReader["NguoiNopTien"] is DBNull))
                this._NguoiNopTien = (string)dataReader["NguoiNopTien"];
            if (!(dataReader["NguoiLapPhieuID"] is DBNull))
                this._NguoiLapPhieuID = (int)dataReader["NguoiLapPhieuID"];
            if (!(dataReader["ThuQuyID"] is DBNull))
                this._ThuQuyID = (int)dataReader["ThuQuyID"];
            if (!(dataReader["DaNhanDuSoTien"] is DBNull))
                this._DaNhanDuSoTien = (string)dataReader["DaNhanDuSoTien"];
            if (!(dataReader["TyGiaNgoaiTe"] is DBNull))
                this._TyGiaNgoaiTe = (double)dataReader["TyGiaNgoaiTe"];
            if (!(dataReader["SoTienQuyDoi"] is DBNull))
                this._SoTienQuyDoi = (double)dataReader["SoTienQuyDoi"];

            this._INgay = NgayLapPhieu.Value.Year * 10000 + NgayLapPhieu.Value.Month * 100 + NgayLapPhieu.Value.Day;
            this._StrLoaiPhieu = (StaticUtil.GetInt(this._LoaiPhieu, 0) == 0 ? "Chi" : "Thu");
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_ThuChiThuQuyID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_ThuChiThuQuyID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
